# -*- coding: utf-8 -*-
"""
Created on Mon Oct 21 19:21:11 2013

@author: s110848 Magdalena Furman

"""

import stockdata
import stocknews
import datetime
from time import mktime
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from collections import Counter
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
import shelve

def get_stock_diff(date, company):
    """ avg(last_week) - avg(this_week) """
    date = datetime.date(date.year, date.month, date.day)
    # last week
    time_delta = datetime.timedelta(days = delta)
    data_last_week = stockdata.get_stock_data(company, date - time_delta, date)
    
    # next week
    data_next_week = stockdata.get_stock_data(company, date, date + time_delta)
    val =  float(data_last_week.mean()) - float(data_next_week.mean())
    return val > 0
    
def get_stock_diff2(date, company):
    """ avg(last_week) - avg(this_week) """
    date = datetime.date(date.year, date.month, date.day)
    # last week
    time_delta = datetime.timedelta(days = delta)
    data_last_week = stockdata.get_stock_data(company, date - time_delta, date)
    
    # next week
    data_next_week = stockdata.get_stock_data(company, date, date + time_delta)
    val =  float(data_last_week.mean()) - float(data_next_week.mean())
    return val > 0
# TRAIN
    
delta = 3    
words = 100
db_articles = shelve.open("stock_articles.she")
db_stock = shelve.open("stock_data.she")
for count, company in enumerate(db_articles):
    news = db_articles[company]        
    allDicts = Counter()
    for single_news in news:
        temp_date = datetime.date.fromtimestamp(mktime(single_news["datetime"]))
        single_news['stock_diff'] = get_stock_diff(temp_date, company)
        allDicts += single_news['wordcounts']
    
#chosen_words = allDicts.most_common(words)
#
#def word_feats(words):
#    return dict([(word, True) for word in words])
#
#feats = [(word_feats(single_news['wordcounts'].keys()), 'neg' if single_news['stock_diff'] else 'pos') for single_news in news]
#cutoff = len(feats)*3/4
#
#trainfeats = feats[:cutoff]
#testfeats = feats[cutoff:]
#print 'train on %d instances, test on %d instances' % (len(trainfeats), len(testfeats))
# 
#classifier = NaiveBayesClassifier.train(trainfeats)
#print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)
#classifier.show_most_informative_features()

# TEST